﻿using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using System;
using NHibernate.Tool.hbm2ddl;
using GIC.DAO.Model;

namespace GIC.DAO.Access
{
    public class NHibernateHelper
    {
        private static ISessionFactory _sessionFactory;

        private static ISessionFactory SessionFactory
        {
            get
            {
                if (_sessionFactory == null)
                    InitializeSessionFactory();

                return _sessionFactory;
            }
        }

        private static void InitializeSessionFactory()
        {
            try
            {
                //string conexao = "Server=175.25.65.23;Database=vin_brasil;User ID=sa;Password=Tech_2009;Trusted_Connection=False;";
                string conexao = "Server=.\\SQLEXPRESS;Database=vin_brasil;User ID=sa;Password=VIN_BRASIL_TECHFOR;Trusted_Connection=False;";
                //string conexao = "Server=VAIO;Database=vin_brasil;User ID=sa;Password=VIN_BRASIL_TECHFOR;Trusted_Connection=False;";
                _sessionFactory = Fluently.Configure()
                    .Database(MsSqlConfiguration.MsSql2008
                                  .ConnectionString(
                                      @conexao)
                                  .ShowSql()
                    )
                    .Mappings(m =>
                              m.FluentMappings
                                  .AddFromAssemblyOf<Parametros>()
                                  .AddFromAssemblyOf<Chassi>()
                                  .AddFromAssemblyOf<BodyNoReenvio>()
                                  .AddFromAssemblyOf<SocketHyundai>()
                                  )
                    .ExposeConfiguration(cfg => new SchemaExport(cfg)
                                                    .Create(false, false))
                    .BuildSessionFactory();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());

            }
        }

        public static ISession OpenSession()
        {
            return SessionFactory.OpenSession();
        }
    }
}
